#pragma once

#include "ITestComDef.h"

#include <vector>

struct IWbemLocator;
struct IWbemServices;

// CLSID 信息
static const CLSID CLSID_TESTCOM = {
	0xb77118c4, 
	0x6cd5, 
	0x40ed, 
	{ 0x9e, 0xa2, 0xe, 0xd3, 0xaf, 0xbc, 0xb5, 0xb3 } 
};
static const WCHAR* CLSID_TESTCOM_STR = L"{B77118C4-6CD5-40ED-9EA2-0ED3AFBCB5B3}";


class CTestCom : public ITestComInterface
{
public:
	explicit CTestCom();
	~CTestCom();

	// 以下继承 IUnknown
	// 查询接口
	HRESULT __stdcall	QueryInterface(REFIID riid, void** ppvObject) override;
	// 增加引用计数
	ULONG __stdcall		AddRef(void) override;
	// 减少引用计数
	ULONG __stdcall		Release(void) override;

	// 以下继承 ITestComInterface
	HRESULT __stdcall GetCpuUsedRate(UINT16* rate) override;
	HRESULT __stdcall GetCpuCoresNum(UINT32* num) override;
	HRESULT __stdcall GetMemTotalSize(UINT64* size) override;
	HRESULT __stdcall GetMemFreeSize(UINT64* size) override;
	HRESULT __stdcall GetBatteryEnergyRemaining(UINT16* percent) override;

private:
	BOOL WbemQuerySingle(const WCHAR* clsName, const WCHAR* prop, VARIANT* variant);
	BOOL WbemQueryMultiple(const WCHAR* clsName, const WCHAR* prop, std::vector<VARIANT>& varArr);
	BOOL CheckFailed(HRESULT hres, const WCHAR* msg);

private:
	// 引用计数
	ULONG			m_refNum;
	// 全局对象数
	static ULONG	g_objNum;

private:
	// WMI 相关成员
	HRESULT			m_hres;
	IWbemLocator*   m_pLoc;
	IWbemServices*  m_pSvr;
};

